package com.hec.yygh.hosp.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hec.yygh.common.result.R;
import com.hec.yygh.hosp.service.HospitalSetService;
import com.hec.yygh.hosp.util.MD5;
import com.hec.yygh.model.hosp.HospitalSet;
import com.hec.yygh.vo.hosp.HospitalSetQueryVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Random;


/**
 * <p>
 * 医院设置表 前端控制器
 * </p>
 *
 * @author hec
 * @since 2022-10-12
 */

@Slf4j
@RestController
@RequestMapping("/admin/hosp/hospitalSet")
public class HospitalSetController {

    @Autowired
    private HospitalSetService hospitalSetService;

    /**
     * 返回医院信息
     */
    @PostMapping("/{current}/{limit}")
    public R getHospitalSet(@PathVariable("current") Integer current,
                  @PathVariable("limit") Integer limit,
                  @RequestBody HospitalSetQueryVo hospitalSetQueryVo
                  ){
        Page<HospitalSet> page = new Page<>(current, limit);
        LambdaQueryWrapper<HospitalSet> queryWrapper = new LambdaQueryWrapper<>();
        String hoscode = hospitalSetQueryVo.getHoscode();
        String hosname = hospitalSetQueryVo.getHosname();
        if(!StringUtils.isEmpty(hosname)){
            queryWrapper.like(HospitalSet::getHosname,hosname);
        }
        if(!StringUtils.isEmpty(hoscode)){
            queryWrapper.eq(HospitalSet::getHoscode,hoscode);
        }
        Page<HospitalSet> pageInfo = hospitalSetService.page(page,queryWrapper);
        List<HospitalSet> records = pageInfo.getRecords();
        return R.ok().data("total", pageInfo.getTotal()).data("rows", records);
    }

    /*
    医院设置添加
     */
    @PostMapping("/save")
    public R saveHospitalSet(@RequestBody HospitalSet hospitalSet){
        hospitalSet.setStatus(0);
        //签名密钥
        Random random = new Random();
        hospitalSet.setSignKey(MD5.encrypt(System.currentTimeMillis()+""+random.nextInt(1000)));
        hospitalSetService.save(hospitalSet);
        return R.ok();
    }

    /*
    回显医院信息
     */
    @GetMapping("/getById/{id}")
    public R getHospitalSetById(@PathVariable("id") Integer id){
        HospitalSet hospitalSetInfo = hospitalSetService.getById(id);
        return R.ok().data("hospitalSet",hospitalSetInfo);
    }

    /*
    更新医院信息
     */
    @PostMapping("/update")
    public R updateHospitalSet(@RequestBody HospitalSet hospitalSet){
        hospitalSetService.updateById(hospitalSet);
        return R.ok();
    }

    /*
    批量删除医院信息
     */
    @DeleteMapping("/batchRemove")
    public R deleteHospitalSetByIds(@RequestBody List<Integer> idList){
        hospitalSetService.removeByIds(idList);
        return R.ok();
    }

    @PutMapping("/lockHospitalSet/{id}/{status}")
    public R putHospitalSetById(@PathVariable("id")Integer id,
                                @PathVariable("status")Integer status){
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        hospitalSet.setStatus(status);
        hospitalSetService.updateById(hospitalSet);
        return R.ok();
    }

}

